{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 中文文件名导致问题\n",
    "\n",
    "直接打开中文文件名文件，可能会触发Pandas的bug"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "OSError",
     "evalue": "Initializing from file failed",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mOSError\u001b[0m                                   Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-97a0eabebf03>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'数据透视表.csv'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36mparser_f\u001b[1;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)\u001b[0m\n\u001b[0;32m    707\u001b[0m                     skip_blank_lines=skip_blank_lines)\n\u001b[0;32m    708\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 709\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0m_read\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    710\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    711\u001b[0m     \u001b[0mparser_f\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m_read\u001b[1;34m(filepath_or_buffer, kwds)\u001b[0m\n\u001b[0;32m    447\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    448\u001b[0m     \u001b[1;31m# Create the parser.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 449\u001b[1;33m     \u001b[0mparser\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTextFileReader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    450\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    451\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mchunksize\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0miterator\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, f, engine, **kwds)\u001b[0m\n\u001b[0;32m    816\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'has_index_names'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkwds\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'has_index_names'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    817\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 818\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_make_engine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mengine\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    819\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    820\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m_make_engine\u001b[1;34m(self, engine)\u001b[0m\n\u001b[0;32m   1047\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_make_engine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mengine\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'c'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1048\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mengine\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'c'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1049\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mCParserWrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1050\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1051\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mengine\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'python'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, src, **kwds)\u001b[0m\n\u001b[0;32m   1693\u001b[0m         \u001b[0mkwds\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'allow_leading_cols'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex_col\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1694\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1695\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_reader\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mparsers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTextReader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1696\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1697\u001b[0m         \u001b[1;31m# XXX\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\parsers.pyx\u001b[0m in \u001b[0;36mpandas._libs.parsers.TextReader.__cinit__\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\parsers.pyx\u001b[0m in \u001b[0;36mpandas._libs.parsers.TextReader._setup_parser_source\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mOSError\u001b[0m: Initializing from file failed"
     ]
    }
   ],
   "source": [
    "data = pd.read_csv('数据透视表.csv')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "比较保险的方法是使用Python原生open，将文件对象传入Pandas方法。\n",
    "但可能因编码问题触发其他问题"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "ename": "UnicodeDecodeError",
     "evalue": "'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-161e25dd0c4f>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'数据透视表.csv'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'rb'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0minf\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mdata\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mread_csv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minf\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mdata\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mhead\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36mparser_f\u001b[1;34m(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)\u001b[0m\n\u001b[0;32m    707\u001b[0m                     skip_blank_lines=skip_blank_lines)\n\u001b[0;32m    708\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 709\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0m_read\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    710\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    711\u001b[0m     \u001b[0mparser_f\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__name__\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m_read\u001b[1;34m(filepath_or_buffer, kwds)\u001b[0m\n\u001b[0;32m    447\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    448\u001b[0m     \u001b[1;31m# Create the parser.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 449\u001b[1;33m     \u001b[0mparser\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTextFileReader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfilepath_or_buffer\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    450\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    451\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mchunksize\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0miterator\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, f, engine, **kwds)\u001b[0m\n\u001b[0;32m    816\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'has_index_names'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkwds\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'has_index_names'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    817\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 818\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_make_engine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mengine\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    819\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    820\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mclose\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m_make_engine\u001b[1;34m(self, engine)\u001b[0m\n\u001b[0;32m   1047\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_make_engine\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mengine\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'c'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1048\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mengine\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'c'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1049\u001b[1;33m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_engine\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mCParserWrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moptions\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1050\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1051\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mengine\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'python'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mf:\\venvs\\data-analysis\\lib\\site-packages\\pandas\\io\\parsers.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, src, **kwds)\u001b[0m\n\u001b[0;32m   1693\u001b[0m         \u001b[0mkwds\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'allow_leading_cols'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mindex_col\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1694\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1695\u001b[1;33m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_reader\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mparsers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mTextReader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwds\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1696\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1697\u001b[0m         \u001b[1;31m# XXX\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\parsers.pyx\u001b[0m in \u001b[0;36mpandas._libs.parsers.TextReader.__cinit__\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;32mpandas\\_libs\\parsers.pyx\u001b[0m in \u001b[0;36mpandas._libs.parsers.TextReader._get_header\u001b[1;34m()\u001b[0m\n",
      "\u001b[1;31mUnicodeDecodeError\u001b[0m: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte"
     ]
    }
   ],
   "source": [
    "with open('数据透视表.csv', 'rb') as inf:\n",
    "    data = pd.read_csv(inf)\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "可以显式指定编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>订购日期</th>\n",
       "      <th>所属区域</th>\n",
       "      <th>产品类别</th>\n",
       "      <th>数量</th>\n",
       "      <th>销售额</th>\n",
       "      <th>成本</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3/21/2007</td>\n",
       "      <td>苏州</td>\n",
       "      <td>宠物用品</td>\n",
       "      <td>16</td>\n",
       "      <td>19,269.69</td>\n",
       "      <td>18,982.85</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4/28/2007</td>\n",
       "      <td>苏州</td>\n",
       "      <td>宠物用品</td>\n",
       "      <td>40</td>\n",
       "      <td>39,465.17</td>\n",
       "      <td>40,893.08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4/28/2007</td>\n",
       "      <td>苏州</td>\n",
       "      <td>宠物用品</td>\n",
       "      <td>20</td>\n",
       "      <td>21,015.94</td>\n",
       "      <td>22,294.09</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5/31/2007</td>\n",
       "      <td>苏州</td>\n",
       "      <td>宠物用品</td>\n",
       "      <td>20</td>\n",
       "      <td>23,710.26</td>\n",
       "      <td>24,318.37</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>6/13/2007</td>\n",
       "      <td>苏州</td>\n",
       "      <td>宠物用品</td>\n",
       "      <td>16</td>\n",
       "      <td>20,015.07</td>\n",
       "      <td>20,256.69</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        订购日期 所属区域  产品类别  数量         销售额          成本\n",
       "0  3/21/2007   苏州  宠物用品  16  19,269.69   18,982.85 \n",
       "1  4/28/2007   苏州  宠物用品  40  39,465.17   40,893.08 \n",
       "2  4/28/2007   苏州  宠物用品  20  21,015.94   22,294.09 \n",
       "3  5/31/2007   苏州  宠物用品  20  23,710.26   24,318.37 \n",
       "4  6/13/2007   苏州  宠物用品  16  20,015.07   20,256.69 "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open('数据透视表.csv', 'rb') as inf:\n",
    "    data = pd.read_csv(inf, encoding='cp936')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 类型推导问题\n",
    "Pandas类型推导可能出现错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>得分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>小明</td>\n",
       "      <td>98</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>小红</td>\n",
       "      <td>87</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>小白</td>\n",
       "      <td>未考核</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   姓名   得分\n",
       "0  小明   98\n",
       "1  小红   87\n",
       "2  小白  未考核"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xls = pd.ExcelFile('test-data1.xlsx')\n",
    "data = xls.parse('Sheet1')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看起来是正常，但是可以看出数据类型是错误的。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     98\n",
       "1     87\n",
       "2    未考核\n",
       "Name: 得分, dtype: object"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['得分']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "解决方法1：替换所有非法值，Pandas会自动使用合适的类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>姓名</th>\n",
       "      <th>得分</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>小明</td>\n",
       "      <td>98.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>小红</td>\n",
       "      <td>87.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>小白</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   姓名    得分\n",
       "0  小明  98.0\n",
       "1  小红  87.0\n",
       "2  小白   NaN"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data['得分']=data['得分'].replace('未考核', np.nan)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "解决方法2：打开时指定应该被替换为na的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    98.0\n",
       "1    87.0\n",
       "2     NaN\n",
       "Name: 得分, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = xls.parse('Sheet1', na_values='未考核')\n",
    "data['得分']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 载入复杂表格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_table('test-data2.txt', index_col='Initial release date')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Code name</th>\n",
       "      <th>Version number</th>\n",
       "      <th>API level</th>\n",
       "      <th>Security patches</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Initial release date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>September 23, 2008</th>\n",
       "      <td>(No codename)</td>\n",
       "      <td>1.0</td>\n",
       "      <td>1</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>February 9, 2009</th>\n",
       "      <td>(Internally known as \"Petit Four\")</td>\n",
       "      <td>1.1</td>\n",
       "      <td>2</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>April 27, 2009</th>\n",
       "      <td>Cupcake</td>\n",
       "      <td>1.5</td>\n",
       "      <td>3</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>September 15, 2009</th>\n",
       "      <td>Donut</td>\n",
       "      <td>1.6</td>\n",
       "      <td>4</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>October 26, 2009</th>\n",
       "      <td>Eclair</td>\n",
       "      <td>2.0 – 2.1</td>\n",
       "      <td>5 – 7</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>May 20, 2010</th>\n",
       "      <td>Froyo</td>\n",
       "      <td>2.2 – 2.2.3</td>\n",
       "      <td>8</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>December 6, 2010</th>\n",
       "      <td>Gingerbread</td>\n",
       "      <td>2.3 – 2.3.7</td>\n",
       "      <td>9 – 10</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>February 22, 2011</th>\n",
       "      <td>Honeycomb</td>\n",
       "      <td>3.0 – 3.2.6</td>\n",
       "      <td>11 – 13</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>October 18, 2011</th>\n",
       "      <td>Ice Cream Sandwich</td>\n",
       "      <td>4.0 – 4.0.4</td>\n",
       "      <td>14 – 15</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>July 9, 2012</th>\n",
       "      <td>Jelly Bean</td>\n",
       "      <td>4.1 – 4.3.1</td>\n",
       "      <td>16 – 18</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>October 31, 2013</th>\n",
       "      <td>KitKat</td>\n",
       "      <td>4.4 – 4.4.4</td>\n",
       "      <td>19 – 20</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>November 12, 2014</th>\n",
       "      <td>Lollipop</td>\n",
       "      <td>5.0 – 5.1.1</td>\n",
       "      <td>21 – 22</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>October 5, 2015</th>\n",
       "      <td>Marshmallow</td>\n",
       "      <td>6.0 – 6.0.1</td>\n",
       "      <td>23</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>August 22, 2016</th>\n",
       "      <td>Nougat</td>\n",
       "      <td>7.0 – 7.1.2</td>\n",
       "      <td>24 – 25</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>August 21, 2017</th>\n",
       "      <td>Oreo</td>\n",
       "      <td>8.0 – 8.1</td>\n",
       "      <td>26 – 27</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>Android P</td>\n",
       "      <td>9</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Developer preview; not yet supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td>Legend:Old versionOlder version, still support...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NaN</th>\n",
       "      <td># ref: https://en.wikipedia.org/wiki/Android_v...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                              Code name  \\\n",
       "Initial release date                                                      \n",
       "September 23, 2008                                        (No codename)   \n",
       "February 9, 2009                     (Internally known as \"Petit Four\")   \n",
       "April 27, 2009                                                  Cupcake   \n",
       "September 15, 2009                                                Donut   \n",
       "October 26, 2009                                                 Eclair   \n",
       "May 20, 2010                                                      Froyo   \n",
       "December 6, 2010                                            Gingerbread   \n",
       "February 22, 2011                                             Honeycomb   \n",
       "October 18, 2011                                     Ice Cream Sandwich   \n",
       "July 9, 2012                                                 Jelly Bean   \n",
       "October 31, 2013                                                 KitKat   \n",
       "November 12, 2014                                              Lollipop   \n",
       "October 5, 2015                                             Marshmallow   \n",
       "August 22, 2016                                                  Nougat   \n",
       "August 21, 2017                                                    Oreo   \n",
       "NaN                                                           Android P   \n",
       "NaN                   Legend:Old versionOlder version, still support...   \n",
       "NaN                   # ref: https://en.wikipedia.org/wiki/Android_v...   \n",
       "\n",
       "                     Version number API level  \\\n",
       "Initial release date                            \n",
       "September 23, 2008              1.0         1   \n",
       "February 9, 2009                1.1         2   \n",
       "April 27, 2009                  1.5         3   \n",
       "September 15, 2009              1.6         4   \n",
       "October 26, 2009          2.0 – 2.1     5 – 7   \n",
       "May 20, 2010            2.2 – 2.2.3         8   \n",
       "December 6, 2010        2.3 – 2.3.7    9 – 10   \n",
       "February 22, 2011       3.0 – 3.2.6   11 – 13   \n",
       "October 18, 2011        4.0 – 4.0.4   14 – 15   \n",
       "July 9, 2012            4.1 – 4.3.1   16 – 18   \n",
       "October 31, 2013        4.4 – 4.4.4   19 – 20   \n",
       "November 12, 2014       5.0 – 5.1.1   21 – 22   \n",
       "October 5, 2015         6.0 – 6.0.1        23   \n",
       "August 22, 2016         7.0 – 7.1.2   24 – 25   \n",
       "August 21, 2017           8.0 – 8.1   26 – 27   \n",
       "NaN                               9       NaN   \n",
       "NaN                             NaN       NaN   \n",
       "NaN                             NaN       NaN   \n",
       "\n",
       "                                          Security patches  \n",
       "Initial release date                                        \n",
       "September 23, 2008                             Unsupported  \n",
       "February 9, 2009                               Unsupported  \n",
       "April 27, 2009                                 Unsupported  \n",
       "September 15, 2009                             Unsupported  \n",
       "October 26, 2009                               Unsupported  \n",
       "May 20, 2010                                   Unsupported  \n",
       "December 6, 2010                               Unsupported  \n",
       "February 22, 2011                              Unsupported  \n",
       "October 18, 2011                               Unsupported  \n",
       "July 9, 2012                                   Unsupported  \n",
       "October 31, 2013                               Unsupported  \n",
       "November 12, 2014                                Supported  \n",
       "October 5, 2015                                  Supported  \n",
       "August 22, 2016                                  Supported  \n",
       "August 21, 2017                                  Supported  \n",
       "NaN                   Developer preview; not yet supported  \n",
       "NaN                                                    NaN  \n",
       "NaN                                                    NaN  "
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用`skipfooter`参数忽略尾注。但因C解析器不支持该参数，会回退到Python解析器，可能导致其他问题。首先在Windows上编码会出现错误，因此显式指定编码为'utf-8'。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "f:\\venvs\\data-analysis\\lib\\site-packages\\ipykernel_launcher.py:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support skipfooter; you can avoid this warning by specifying engine='python'.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Code name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>(No codename)</th>\n",
       "      <th>1.0</th>\n",
       "      <th>September 23, 2008</th>\n",
       "      <th>1</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>(Internally known as \"Petit Four\")</th>\n",
       "      <th>1.1</th>\n",
       "      <th>February 9, 2009</th>\n",
       "      <th>2</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Cupcake</th>\n",
       "      <th>1.5</th>\n",
       "      <th>April 27, 2009</th>\n",
       "      <th>3</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Donut</th>\n",
       "      <th>1.6</th>\n",
       "      <th>September 15, 2009</th>\n",
       "      <th>4</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Eclair</th>\n",
       "      <th>2.0 – 2.1</th>\n",
       "      <th>October 26, 2009</th>\n",
       "      <th>5 – 7</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Froyo</th>\n",
       "      <th>2.2 – 2.2.3</th>\n",
       "      <th>May 20, 2010</th>\n",
       "      <th>8</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Gingerbread</th>\n",
       "      <th>2.3 – 2.3.7</th>\n",
       "      <th>December 6, 2010</th>\n",
       "      <th>9 – 10</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Honeycomb</th>\n",
       "      <th>3.0 – 3.2.6</th>\n",
       "      <th>February 22, 2011</th>\n",
       "      <th>11 – 13</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ice Cream Sandwich</th>\n",
       "      <th>4.0 – 4.0.4</th>\n",
       "      <th>October 18, 2011</th>\n",
       "      <th>14 – 15</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Jelly Bean</th>\n",
       "      <th>4.1 – 4.3.1</th>\n",
       "      <th>July 9, 2012</th>\n",
       "      <th>16 – 18</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>KitKat</th>\n",
       "      <th>4.4 – 4.4.4</th>\n",
       "      <th>October 31, 2013</th>\n",
       "      <th>19 – 20</th>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Lollipop</th>\n",
       "      <th>5.0 – 5.1.1</th>\n",
       "      <th>November 12, 2014</th>\n",
       "      <th>21 – 22</th>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Marshmallow</th>\n",
       "      <th>6.0 – 6.0.1</th>\n",
       "      <th>October 5, 2015</th>\n",
       "      <th>23</th>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Nougat</th>\n",
       "      <th>7.0 – 7.1.2</th>\n",
       "      <th>August 22, 2016</th>\n",
       "      <th>24 – 25</th>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Oreo</th>\n",
       "      <th>8.0 – 8.1</th>\n",
       "      <th>August 21, 2017</th>\n",
       "      <th>26 – 27</th>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Android P</th>\n",
       "      <th>9</th>\n",
       "      <th>NaN</th>\n",
       "      <th>NaN</th>\n",
       "      <td>Developer preview; not yet supported</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                                      Code name\n",
       "(No codename)                      1.0         September 23, 2008 1                                 Unsupported\n",
       "(Internally known as \"Petit Four\") 1.1         February 9, 2009   2                                 Unsupported\n",
       "Cupcake                            1.5         April 27, 2009     3                                 Unsupported\n",
       "Donut                              1.6         September 15, 2009 4                                 Unsupported\n",
       "Eclair                             2.0 – 2.1   October 26, 2009   5 – 7                             Unsupported\n",
       "Froyo                              2.2 – 2.2.3 May 20, 2010       8                                 Unsupported\n",
       "Gingerbread                        2.3 – 2.3.7 December 6, 2010   9 – 10                            Unsupported\n",
       "Honeycomb                          3.0 – 3.2.6 February 22, 2011  11 – 13                           Unsupported\n",
       "Ice Cream Sandwich                 4.0 – 4.0.4 October 18, 2011   14 – 15                           Unsupported\n",
       "Jelly Bean                         4.1 – 4.3.1 July 9, 2012       16 – 18                           Unsupported\n",
       "KitKat                             4.4 – 4.4.4 October 31, 2013   19 – 20                           Unsupported\n",
       "Lollipop                           5.0 – 5.1.1 November 12, 2014  21 – 22                             Supported\n",
       "Marshmallow                        6.0 – 6.0.1 October 5, 2015    23                                  Supported\n",
       "Nougat                             7.0 – 7.1.2 August 22, 2016    24 – 25                             Supported\n",
       "Oreo                               8.0 – 8.1   August 21, 2017    26 – 27                             Supported\n",
       "Android P                          9           NaN                NaN      Developer preview; not yet supported"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_table('test-data2.txt', skipfooter=2, encoding='utf-8')\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "头部解析明显出了错误，因此改用手动指定头部"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "f:\\venvs\\data-analysis\\lib\\site-packages\\ipykernel_launcher.py:1: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support skipfooter; you can avoid this warning by specifying engine='python'.\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Code name</th>\n",
       "      <th>Version number</th>\n",
       "      <th>Initial release date</th>\n",
       "      <th>API level</th>\n",
       "      <th>Security patches</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(No codename)</td>\n",
       "      <td>1.0</td>\n",
       "      <td>September 23, 2008</td>\n",
       "      <td>1</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(Internally known as \"Petit Four\")</td>\n",
       "      <td>1.1</td>\n",
       "      <td>February 9, 2009</td>\n",
       "      <td>2</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Cupcake</td>\n",
       "      <td>1.5</td>\n",
       "      <td>April 27, 2009</td>\n",
       "      <td>3</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Donut</td>\n",
       "      <td>1.6</td>\n",
       "      <td>September 15, 2009</td>\n",
       "      <td>4</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eclair</td>\n",
       "      <td>2.0 – 2.1</td>\n",
       "      <td>October 26, 2009</td>\n",
       "      <td>5 – 7</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Froyo</td>\n",
       "      <td>2.2 – 2.2.3</td>\n",
       "      <td>May 20, 2010</td>\n",
       "      <td>8</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Gingerbread</td>\n",
       "      <td>2.3 – 2.3.7</td>\n",
       "      <td>December 6, 2010</td>\n",
       "      <td>9 – 10</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Honeycomb</td>\n",
       "      <td>3.0 – 3.2.6</td>\n",
       "      <td>February 22, 2011</td>\n",
       "      <td>11 – 13</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Ice Cream Sandwich</td>\n",
       "      <td>4.0 – 4.0.4</td>\n",
       "      <td>October 18, 2011</td>\n",
       "      <td>14 – 15</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Jelly Bean</td>\n",
       "      <td>4.1 – 4.3.1</td>\n",
       "      <td>July 9, 2012</td>\n",
       "      <td>16 – 18</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>KitKat</td>\n",
       "      <td>4.4 – 4.4.4</td>\n",
       "      <td>October 31, 2013</td>\n",
       "      <td>19 – 20</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Lollipop</td>\n",
       "      <td>5.0 – 5.1.1</td>\n",
       "      <td>November 12, 2014</td>\n",
       "      <td>21 – 22</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Marshmallow</td>\n",
       "      <td>6.0 – 6.0.1</td>\n",
       "      <td>October 5, 2015</td>\n",
       "      <td>23</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Nougat</td>\n",
       "      <td>7.0 – 7.1.2</td>\n",
       "      <td>August 22, 2016</td>\n",
       "      <td>24 – 25</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Oreo</td>\n",
       "      <td>8.0 – 8.1</td>\n",
       "      <td>August 21, 2017</td>\n",
       "      <td>26 – 27</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Android P</td>\n",
       "      <td>9</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Developer preview; not yet supported</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             Code name Version number Initial release date  \\\n",
       "0                        (No codename)            1.0   September 23, 2008   \n",
       "1   (Internally known as \"Petit Four\")            1.1     February 9, 2009   \n",
       "2                              Cupcake            1.5       April 27, 2009   \n",
       "3                                Donut            1.6   September 15, 2009   \n",
       "4                               Eclair      2.0 – 2.1     October 26, 2009   \n",
       "5                                Froyo    2.2 – 2.2.3         May 20, 2010   \n",
       "6                          Gingerbread    2.3 – 2.3.7     December 6, 2010   \n",
       "7                            Honeycomb    3.0 – 3.2.6    February 22, 2011   \n",
       "8                   Ice Cream Sandwich    4.0 – 4.0.4     October 18, 2011   \n",
       "9                           Jelly Bean    4.1 – 4.3.1         July 9, 2012   \n",
       "10                              KitKat    4.4 – 4.4.4     October 31, 2013   \n",
       "11                            Lollipop    5.0 – 5.1.1    November 12, 2014   \n",
       "12                         Marshmallow    6.0 – 6.0.1      October 5, 2015   \n",
       "13                              Nougat    7.0 – 7.1.2      August 22, 2016   \n",
       "14                                Oreo      8.0 – 8.1      August 21, 2017   \n",
       "15                           Android P              9                  NaN   \n",
       "\n",
       "   API level                      Security patches  \n",
       "0          1                           Unsupported  \n",
       "1          2                           Unsupported  \n",
       "2          3                           Unsupported  \n",
       "3          4                           Unsupported  \n",
       "4      5 – 7                           Unsupported  \n",
       "5          8                           Unsupported  \n",
       "6     9 – 10                           Unsupported  \n",
       "7    11 – 13                           Unsupported  \n",
       "8    14 – 15                           Unsupported  \n",
       "9    16 – 18                           Unsupported  \n",
       "10   19 – 20                           Unsupported  \n",
       "11   21 – 22                             Supported  \n",
       "12        23                             Supported  \n",
       "13   24 – 25                             Supported  \n",
       "14   26 – 27                             Supported  \n",
       "15       NaN  Developer preview; not yet supported  "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_table('test-data2.txt', skipfooter=2, encoding='utf-8', header=None, skiprows=1)\n",
    "data.columns='Code name\tVersion number\tInitial release date\tAPI level\tSecurity patches'.split('\\t')\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "另一个workaround是，只读取需要的行数，避免使用`skipfooter`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Code name</th>\n",
       "      <th>Version number</th>\n",
       "      <th>Initial release date</th>\n",
       "      <th>API level</th>\n",
       "      <th>Security patches</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>(No codename)</td>\n",
       "      <td>1.0</td>\n",
       "      <td>September 23, 2008</td>\n",
       "      <td>1</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>(Internally known as \"Petit Four\")</td>\n",
       "      <td>1.1</td>\n",
       "      <td>February 9, 2009</td>\n",
       "      <td>2</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Cupcake</td>\n",
       "      <td>1.5</td>\n",
       "      <td>April 27, 2009</td>\n",
       "      <td>3</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Donut</td>\n",
       "      <td>1.6</td>\n",
       "      <td>September 15, 2009</td>\n",
       "      <td>4</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Eclair</td>\n",
       "      <td>2.0 – 2.1</td>\n",
       "      <td>October 26, 2009</td>\n",
       "      <td>5 – 7</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>Froyo</td>\n",
       "      <td>2.2 – 2.2.3</td>\n",
       "      <td>May 20, 2010</td>\n",
       "      <td>8</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>Gingerbread</td>\n",
       "      <td>2.3 – 2.3.7</td>\n",
       "      <td>December 6, 2010</td>\n",
       "      <td>9 – 10</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>Honeycomb</td>\n",
       "      <td>3.0 – 3.2.6</td>\n",
       "      <td>February 22, 2011</td>\n",
       "      <td>11 – 13</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>Ice Cream Sandwich</td>\n",
       "      <td>4.0 – 4.0.4</td>\n",
       "      <td>October 18, 2011</td>\n",
       "      <td>14 – 15</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>Jelly Bean</td>\n",
       "      <td>4.1 – 4.3.1</td>\n",
       "      <td>July 9, 2012</td>\n",
       "      <td>16 – 18</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>KitKat</td>\n",
       "      <td>4.4 – 4.4.4</td>\n",
       "      <td>October 31, 2013</td>\n",
       "      <td>19 – 20</td>\n",
       "      <td>Unsupported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>Lollipop</td>\n",
       "      <td>5.0 – 5.1.1</td>\n",
       "      <td>November 12, 2014</td>\n",
       "      <td>21 – 22</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>Marshmallow</td>\n",
       "      <td>6.0 – 6.0.1</td>\n",
       "      <td>October 5, 2015</td>\n",
       "      <td>23</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>Nougat</td>\n",
       "      <td>7.0 – 7.1.2</td>\n",
       "      <td>August 22, 2016</td>\n",
       "      <td>24 – 25</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>Oreo</td>\n",
       "      <td>8.0 – 8.1</td>\n",
       "      <td>August 21, 2017</td>\n",
       "      <td>26 – 27</td>\n",
       "      <td>Supported</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>Android P</td>\n",
       "      <td>9</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Developer preview; not yet supported</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                             Code name Version number Initial release date  \\\n",
       "0                        (No codename)            1.0   September 23, 2008   \n",
       "1   (Internally known as \"Petit Four\")            1.1     February 9, 2009   \n",
       "2                              Cupcake            1.5       April 27, 2009   \n",
       "3                                Donut            1.6   September 15, 2009   \n",
       "4                               Eclair      2.0 – 2.1     October 26, 2009   \n",
       "5                                Froyo    2.2 – 2.2.3         May 20, 2010   \n",
       "6                          Gingerbread    2.3 – 2.3.7     December 6, 2010   \n",
       "7                            Honeycomb    3.0 – 3.2.6    February 22, 2011   \n",
       "8                   Ice Cream Sandwich    4.0 – 4.0.4     October 18, 2011   \n",
       "9                           Jelly Bean    4.1 – 4.3.1         July 9, 2012   \n",
       "10                              KitKat    4.4 – 4.4.4     October 31, 2013   \n",
       "11                            Lollipop    5.0 – 5.1.1    November 12, 2014   \n",
       "12                         Marshmallow    6.0 – 6.0.1      October 5, 2015   \n",
       "13                              Nougat    7.0 – 7.1.2      August 22, 2016   \n",
       "14                                Oreo      8.0 – 8.1      August 21, 2017   \n",
       "15                           Android P              9                  NaN   \n",
       "\n",
       "   API level                      Security patches  \n",
       "0          1                           Unsupported  \n",
       "1          2                           Unsupported  \n",
       "2          3                           Unsupported  \n",
       "3          4                           Unsupported  \n",
       "4      5 – 7                           Unsupported  \n",
       "5          8                           Unsupported  \n",
       "6     9 – 10                           Unsupported  \n",
       "7    11 – 13                           Unsupported  \n",
       "8    14 – 15                           Unsupported  \n",
       "9    16 – 18                           Unsupported  \n",
       "10   19 – 20                           Unsupported  \n",
       "11   21 – 22                             Supported  \n",
       "12        23                             Supported  \n",
       "13   24 – 25                             Supported  \n",
       "14   26 – 27                             Supported  \n",
       "15       NaN  Developer preview; not yet supported  "
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_table('test-data2.txt', nrows=16)\n",
    "data"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
